/**
* 基础菜单 商品管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>酒店管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="searchForm" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchForm.hotelName" placeholder="输入酒店名"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="searchForm.hotelAddress" placeholder="输入酒店地址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
              element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="hotelName" label="酒店名">
      </el-table-column>
      <el-table-column sortable prop="hotelIntroduce" label="简介" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="hotelPraise" label="价格">
      </el-table-column>
      <el-table-column sortable prop="hotelAddress" label="地址" show-overflow-tooltip>
      </el-table-column>
      <el-table-column sortable prop="hotelTel" label="电话">
      </el-table-column>

      <el-table-column sortable prop="hotelImg" label="图片">
        　　
        <template slot-scope="scope">
          <el-image :src="aa+scope.row.hotelImg"></el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" ref="editForm">
        <el-form-item label="酒店名" prop="deptName">
          <el-input size="small" v-model="editForm.hotelName" auto-complete="off"
                    placeholder="请输入酒店名"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="deptNo" >
          <el-input size="small" v-model="editForm.hotelIntroduce" auto-complete="off" type="textarea"
                    placeholder="请输入简介"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="deptNo">
          <el-input size="small" v-model="editForm.hotelPraise" auto-complete="off"
                    placeholder="请输入价格"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="deptNo">
          <el-input size="small" v-model="editForm.hotelAddress" auto-complete="off"
                    placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="deptNo">
          <el-input size="small" v-model="editForm.hotelTel" auto-complete="off"
                    placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="deptNo">
          <el-upload
            ref="upload"
            class="upload-demo"
            action=""
            :file-list="fileList"
            :auto-upload="false"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-change="onchange"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Request from "../../api/request";

export default {
  name: 'Hotel',
  data() {
    return {
      aa: 'data:image/png;base64,',
      fileList: [],

      currentPage: 1,
      size: 5,
      total: 0,
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',

      searchForm: {
        hotelName: '',
        hotelAddress: ''


      },
      editForm: {
        hotelId: '',
        hotelName: '',
        hotelIntroduce: '',
        hotelPraise: '',
        hotelAddress: '',
        hotelTel: '',
        hotelImg: '',

      },
      // rules表单验证
      /*   rules: {
           deptName: [
             {required: true, message: '请输入部门名称', trigger: 'blur'}
           ],
           deptNo: [{required: true, message: '请输入部门代码', trigger: 'blur'}]
         },*/

      // 删除部门

      listData: [], //用户数据
      // 分页参数

    }
  },
  // 注册组件

  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  mounted() {
    this.select()
  },


  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    onchange(file, fileList) {
      this.fileList = []

      this.fileList.push(file)
    },

    handleRemove(file, fileList) {
      this.fileList = []
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    // 获取公司列表
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.select()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.select()
    },
    select() {
      Request.get(
        "/hotel?current=" + this.currentPage +
        "&size=" + this.size +
        "&hotelName=" + this.searchForm.hotelName +
        "&hotelAddress=" + this.searchForm.hotelAddress)
        .then(res => {
          this.loading = false

          this.listData = res.data.data.records
          // 分页赋值
          this.currentPage = res.data.data.current
          this.size = res.data.data.size
          this.total = res.data.data.total

        })
    },

    // 分页插件事件

    // 搜索事件
    search() {
      this.select()
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.hotelId = row.hotelId
        this.editForm.hotelName = row.hotelName
        this.editForm.hotelIntroduce = row.hotelIntroduce
        this.editForm.hotelPraise = row.hotelPraise
        this.editForm.hotelAddress = row.hotelAddress
        this.editForm.hotelTel = row.hotelTel
        this.editForm.hotelImg = row.hotelImg
        this.fileList = []
        this.fileList.push({url: this.aa + row.hotelImg})
      } else {
        this.title = '添加'
        this.editForm.hotelId = ''
        this.editForm.hotelName = ''
        this.editForm.hotelIntroduce = ''
        this.editForm.hotelPraise = ''
        this.editForm.hotelAddress = ''
        this.editForm.hotelTel = ''
        this.editForm.hotelImg = ''
        this.fileList = []

      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {

      let formData = new FormData();
      formData.append('hotel', JSON.stringify(this.editForm))

      if (this.fileList[0].raw != undefined)
        formData.append('multipartFile', this.fileList[0].raw)

      let res
      if (this.title == '添加') res = Request.post("hotel", formData)
      else {
        if (this.fileList[0].raw != undefined) res = Request.put("hotel", formData)
        else {

          res = Request.put("hotel/edit", formData)

        }
      }
      res.then(res => {
        this.editFormVisible = false
        this.loading = false
        if (res.data.data) {
          this.select();
          this.$message({
            type: 'success',
            message: '保存成功！'
          })
        } else {
          this.$message({
            type: 'info',
            message: ''
          })
        }
      })
        .catch(err => {
          this.editFormVisible = false
          this.loading = false
          this.$message.error('保存失败，请稍后再试！')
        })
    },
    // 删除公司
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          Request.delete("hotel", {data: row})
            .then(res => {
              if (res.data.data) {
                this.select();
                this.$message({
                  type: 'success',
                  message: '删除成功'
                })
                this.select();
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.fileList = []
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>


